import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
const state = {
  token: getToken() || ''// 从缓存中获取token
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    setToken(token)
    console.log(state.token)
  },
  REMOVE_TOKEN: (state) => {
    state.token = null
    removeToken()
  }
}
const actions = {
  login(context, data) {
    login(data).then(res => {
      console.log(res)
      context.commit('SET_TOKEN', res)
    }).catch(err => {
      console.log(err)
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
